import request from '@/router/axios';
import Mock from 'mockjs'; 
import { Random } from 'mockjs'

let index='';
Random.extend({
    // 自定义函数名: function 函数  
    telFn: function() {    
      const phonePrefix = ['132', '135', '189','156','138','181','186']
      const index = Math.floor(Math.random() * phonePrefix.length)
      var phone = phonePrefix[index] + Mock.mock(/\d{8}/);
      return this.pick(phone)
    },
    apptypeFn: function() {    
        index = Math.floor(Math.random() * 4)
        return this.pick(index)
    },
    approFn: function() {    
        let num = index==3?0:1;
        return this.pick(num)
    }
})


//获取时间

function getCurrentDate(){
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth();//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    var sec = now.getSeconds();//得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    return time;
}

let list = Mock.mock({
    "tableList|10-30":[
        {   
            username:'@first', // 账号名称
            'id|1-399':300, // 服务单号
            "amount|1-3200.2": 1, // 退款金额
            phone:'@telFn()',  //手机号
            apptype:"@apptypeFn()", // 审批状态 0退货中  1拒接  2已完成  3待处理 
            appro:"@approFn()",//操作审批 0审批  1已审批
            addTime:'@datetime', //申请时间
            endTime:'@datetime', //处理时间
        }
    ]
})
// username,id,amount,phone,apptype,appro,addTime,endTime

//数据列表
export const approvalList = (data) =>request({
    url:'/approvalList',
    method:'get',
    data
})

Mock.mock(/\/approvalList/,config=>{
    let {pagenum,pagesize} = JSON.parse(config.body);
    let _list = list.tableList;
    let total = _list.length;
    if(pagenum && pagesize){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                total,
                user,pagenum,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
            
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
   
})

//删除数据
export const detApproval = (data) =>request({
    url:'/detApproval',
    mothed:'delete',
    data
})
Mock.mock(/\/detApproval/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
    
})

//查询 根据 服务单号  手机号  处理状态 
// id phone apptype
export const queryApproval = (data) =>request({
    url:'/queryApproval',
    mothed:'get',
    data
})
Mock.mock(/\/queryApproval/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.id || datas.phone || datas.apptype){
        if(datas.id){
            arr = _list.filter((item,index)=>JSON.stringify(item.id).indexOf(datas.id)>=0);
        }
        if(datas.phone){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.phone.indexOf(datas.phone)!=-1);
        }
        if(datas.apptype.length>0){
            let str = JSON.stringify(datas.apptype)
            arr = (arr.length==0?_list:arr).filter((item,index)=>str.indexOf(item.apptype)>=0);
        }else if(datas.apptype.length=0){
            return {
                meta:{
                    msg:'参数错误',
                    status:400
                }
            }
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'查询成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 审批
export const editApproval = (data) =>request({
    url:'/editApproval',
    mothed:'put',
    data
})
Mock.mock(/\/editApproval/,config=>{
    let datas = JSON.parse(config.body);
    let {id,apptype} = datas;
    let _list = list.tableList;
    let num = 0;
    if(id && (apptype==0 || apptype==1)){
        for(let item of _list){
            if(item.id==id){
                if(item.appro == 0){
                    item.appro = 1;
                    item.apptype = apptype;
                }else{
                    num = 1
                }
            }
        }
        if(num == 1){
            return {
                meta:{
                    msg:'错误',
                    status:400
                }
            }
        }else{
            return {
                meta:{
                    msg:'审批成功',
                    status:200
                }
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
    
})
